// pages/index/activityList/activityList.js
const app = getApp();
import { actionsIndex, actionPost } from '../../../api/api.js';
var WxParse = require('../../../utils/wxParse/wxParse');
Page({

  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '活动 ', //导航栏 中间的标题
      img: "../../image/back.png"
    },
    aid:"",
    params: {},
    height: app.globalData.height * 2 + 20,
    fromData: {
      username: '',
      mobile: ''
    },
    imgUrl: app.globalData.imgUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    var that = this;
    // console.log(opt);
    if(opt.id){
      let id = opt.id;
      this.setData({
        aid:id
      });

      // 方法带参数 actionsIndex({id:id})
      // 方法不带参数  actionsIndex()
      app.showLoading();
      actionsIndex({id:id}).then(res =>{
        app.hideLoading();
        // console.log(res)
        let list = res.data;
        // console.log(list)
        that.setData({
          params:list
        });
        const htmler = list.content;
        WxParse.wxParse('htmler', 'html', htmler, that, 5);
        // console.log(this.data.params);
      })
    }
  },

  formSubmit: function(e) {
    const data = e.detail.value;
    data.openid = app.globalData.openid;
    data.action_id = this.data.params.action_id;
    if (data.username == '') {
      app.showToast('请填写姓名!');
      return;
    }
    if (data.mobile == '') {
      app.showToast('请填写手机')
      return;
    }
    wx.showLoading({
      title: '提交中',
    });
    actionPost(data).then(res => {
      // console.log(res);
      if (res.code == 0) {
        this.showToast('报名成功');
      } else if (res.code == -1) {
        this.showToast(res.msg);
      }
    })
  },
  showToast (msg) {
    wx.hideLoading();
    wx.showToast({
      title: msg,
      icon: 'success',
      duration: 1000
    });
    this.setData({
      'fromData.username': '',
      'fromData.mobile': ''
    })
  }
  
})